package com.kun.peng.generator.rest;

import com.kun.peng.generator.domain.ColumnInfo;
import com.kun.peng.generator.service.GenColumnInfoService;
import com.kun.peng.exception.BadRequestException;
import com.kun.peng.utils.PageUtil;
import com.kun.peng.generator.service.GenConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

/**
 * Created on 2021/11/18.
 *
 * @author xuebaopeng
 * Description
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/generator")
@Api(tags = "代码生成管理")
public class GenColumnInfoController {

    @Value("${generator.enabled}")
    private Boolean generatorEnabled;

    private final GenColumnInfoService genColumnInfoService;
    private final GenConfigService genConfigService;

    @ApiOperation("查询数据库数据")
    @GetMapping(value = "/tables")
    public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name,
                                              @RequestParam(defaultValue = "0")Integer page,
                                              @RequestParam(defaultValue = "10")Integer size) throws SQLException {
        int[] startEnd = PageUtil.transToStartEnd(page, size);
        return new ResponseEntity<>(genColumnInfoService.getTables(name,startEnd), HttpStatus.OK);
    }
//    @ApiOperation("查询数据库数据")
//    @PostMapping(value = "/tables")
//    public ResponseEntity<Object> queryTables(@RequestBody MyPage<String,TableInfo> page) throws SQLException {
//        return new ResponseEntity<>(genColumnInfoService.getTables(page), HttpStatus.OK);
//    }

    @ApiOperation("查询字段数据")
    @GetMapping(value = "/columns")
    public ResponseEntity<Object> queryColumns(@RequestParam String tableName) throws SQLException {
        List<ColumnInfo> columnInfos = genColumnInfoService.getColumns(tableName);
        return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
    }
    @ApiOperation("保存字段数据")
    @PutMapping
    public ResponseEntity<HttpStatus> save(@RequestBody List<ColumnInfo> columnInfos){
        genColumnInfoService.save(columnInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "sync")
    public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables) throws SQLException {
        for (String table : tables) {
            genColumnInfoService.sync(genColumnInfoService.getColumns(table), genColumnInfoService.query(table));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response) throws SQLException {
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
        }
        switch (type){
            // 生成代码
            case 0: genColumnInfoService.generator(genConfigService.find(tableName), genColumnInfoService.getColumns(tableName));
                break;
            // 预览
            case 1: return genColumnInfoService.preview(genConfigService.find(tableName), genColumnInfoService.getColumns(tableName));
            // 打包
            case 2: genColumnInfoService.download(genConfigService.find(tableName), genColumnInfoService.getColumns(tableName), request, response);
                break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
